package com.ys.controller.web.overseas;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.overseas.TosProductSelection;
import com.ys.entity.overseas.TosProductSelectionItem;
import com.ys.entity.sys.YsProcInst;
import com.ys.service.sys.CommonApiService;
import com.ys.util.DateUtil;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: fxz
 * @CreateTime: 2024-12-17
 * @Description: HW商品选品库WEB
 * @Version: 1.5
 */
@Controller
@RequestMapping("/web/overseas/tosProductSelection")
public class WebTosProductSelectionController {
    private static final Logger logger = LoggerFactory.getLogger(WebTosProductSelectionController.class);

    @Autowired
    private CommonApiService<TosProductSelection> commonApiService;

    Long MENUID = MenuId.TOS_ProductSelection;

    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> params){
        try {
            String bid = commonApiService.getCommonNextBid(MENUID);
            TosProductSelection tosProductSelection = new TosProductSelection()
                    .setBid(bid).setStatus(1);
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENUID, new ArrayList<Map<String, Object>>());
            return commonApiService.toAdd(model, tosProductSelection, MENUID, null, subGridConfig);
        }catch (Exception e) {
            e.printStackTrace();
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> params){
        try {
            if (params.get("bid") == null){
                return W.error(model, "单号不能为空");
            }
            TosProductSelection tosProductSelection = new TosProductSelection().setBid(params.get("bid").toString()).queryFirst();
            List<Map<String, Object>> tosProductSelectionList = new TosProductSelectionItem()
                    .addLeftJoin("TOS_PlatformInformation C ON C.Id = A.PlatformId ")//平台信息维护表
                    .addField("C.HwChannel AS hwChannel")//渠道
                    .addField("C.PlatformCode AS platformCode")//平台
                    .addField("C.HwName AS hwName")//区域
                    .addField("C.RegionName AS regionName")//站点
                    .addField("C.CurrencyCode AS currencyCode")//币种
                    .setPid(tosProductSelection.getId()).queryMap();

            YsProcInst ysProcInst = new YsProcInst().setBid(tosProductSelection.getBid()).queryFirst();

            Map<String, Object> extraParam = new HashMap<String, Object>();

            extraParam.put("auditingTime", DateUtil.getYYYY_MM_DD_HH_mm_ss(ysProcInst.getUpdateTime()));
            extraParam.put("createTime1", DateUtil.getYYYY_MM_DD_HH_mm_ss(tosProductSelection.getCreateTime()));

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENUID, tosProductSelectionList);
            return commonApiService.toEdit(model, tosProductSelection.getId(), new TosProductSelection(), MENUID, extraParam, subGridConfig);
        }catch (Exception e) {
            e.printStackTrace();
            return W.error(model, e.getMessage());
        }
    }
}
